package com.itheima.exerciseTest.method40_49.method47;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.util.Scanner;

// 客户端
public class ClientDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientDemo.class);
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        Socket s = null;
        try {
            s = new Socket("192.168.80.1", 10011);
        } catch (ConnectException e) {
            LOGGER.info("与服务器连接失败" + e);
        }

        // 接收服务器响应
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = br.readLine();
        System.out.println(str);
//        String str;
//        while ((str = br.readLine()) != null){
//            System.out.println(str);
//        }


        // 给服务器发送文本文件
        // 读取本地文件
        BufferedReader br2 = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\aheima\\模拟考试\\考试\\试题B\\test.txt"), "GBK"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String str2;
        while ((str2 = br2.readLine()) != null) {
            bw.write(str2);
            bw.newLine();
            bw.flush();
        }
        s.shutdownOutput();

        // 接收服务器响应
        str = br.readLine();
        System.out.println(str);

        if (br2 != null) {
            br2.close();
        }

        if (s != null) {
            s.close();
        }
    }

}
